package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.danmo.domain.DanmoInformation;
import com.muyu.danmo.domain.DanmoInformationBar;
import com.muyu.danmo.domain.DanmoInformationContent;
import com.muyu.danmo.domain.DanmoInformationPhotos;
import com.muyu.danmo.service.DanmoService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * DanmoServiceImpl
 *
 * @author LeYang
 * on 2024/11/18
 */
@Service
public class DanmoServiceImpl implements DanmoService {
    @Autowired
    private DanmoInformationServiceImpl danmoInformationService;

    @Autowired
    private DanmoInformationBarServiceImpl danmoInformationBarService;
    @Autowired
    private DanmoInformationPhotosServiceImpl danmoInformationPhotosServiceImpl;

    @Autowired
    private DanmoInformationContentServiceImpl danmoInformationContentService;

    @Override
    public Result add(String name) {
        try (InputStream inputStream = new FileInputStream(name);
             Workbook workbook = new XSSFWorkbook(inputStream)) {
            boolean isFirstRow = true;
            Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
            String previousName = null;
            List<DanmoInformationContent> danmoInformationContents = new ArrayList<>();
            DanmoInformation danmoInformation = null;
            DanmoInformationBar danmoInformationBar = null;

            for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { // 从第二行开始，索引从0开始，所以第二行的索引是1
                Row row = sheet.getRow(rowIndex);
                // 获取单元格数据
                Cell chineseName = row.getCell(0);
                Cell fontSize = row.getCell(1);
                Cell membership = row.getCell(2);
                Cell city = row.getCell(3);
                Cell district = row.getCell(4);
                Cell avatar = row.getCell(5);
                Cell foreignName = row.getCell(6);
                Cell nationality = row.getCell(7);
                Cell national = row.getCell(8);
                Cell birthplace = row.getCell(9);
                Cell dateBirth = row.getCell(10);
                Cell graduateSchool = row.getCell(11);
                Cell vocational = row.getCell(12);
                Cell representativeWorks = row.getCell(13);
                Cell mainAchievements = row.getCell(14);
                Cell positions = row.getCell(15);
                Cell parent = row.getCell(16);
                Cell rich = row.getCell(17);
                Cell atlas = row.getCell(18);
                Cell sort = row.getCell(19);
                Cell association = row.getCell(20);
                Cell profile = row.getCell(21);
                Cell year = row.getCell(22);
                Cell whichSession = row.getCell(23);
                Cell duties = row.getCell(24);

                if (isFirstRow || !String.valueOf(chineseName).equals(previousName) || chineseName == null) {
                    // 新的 `chineseName` 或者是第一行数据，或者 `chineseName` 为 null
                    if (danmoInformation != null && !danmoInformationContents.isEmpty()) {
                        System.out.println("------------------------------------------------");
                        System.out.println("danmoInformationContents"+danmoInformationContents);
                        System.out.println("------------------------------------------------");
//                        danmoInformationContentService.add(danmoInformationContents);
                        danmoInformationContents = new ArrayList<>();
                    }

                    if (chineseName != null || chineseName.equals("")) {
                        danmoInformation = new DanmoInformation();
                        danmoInformation.setYear(String.valueOf(year));
                        danmoInformation.setAssociation(Long.valueOf(1));
                        danmoInformation.setAddressEntry(String.valueOf(membership));
                        danmoInformation.setAddressCity(String.valueOf(city));
                        danmoInformation.setAddressCounty(String.valueOf(district));
//                        danmoInformationService.insertDanmoInformation(danmoInformation);
                        System.out.println("danmoInformation"+danmoInformation);
                        danmoInformationBar = new DanmoInformationBar();
                        danmoInformationBar.setChineseName(String.valueOf(chineseName));
                        danmoInformationBar.setFontSize(String.valueOf(fontSize));
                        danmoInformationBar.setNationality(String.valueOf(nationality));
                        danmoInformationBar.setNational(String.valueOf(national));
                        danmoInformationBar.setBirthplace(String.valueOf(birthplace));
                        danmoInformationBar.setDateBirth(String.valueOf(dateBirth));
                        danmoInformationBar.setForeignName(String.valueOf(foreignName));
                        danmoInformationBar.setGraduateSchool(String.valueOf(graduateSchool));
                        danmoInformationBar.setVocational(String.valueOf(vocational));
                        danmoInformationBar.setRepresentativeWorks(String.valueOf(representativeWorks));
                        danmoInformationBar.setMainAchievements(String.valueOf(mainAchievements));
                        danmoInformationBar.setPositions(String.valueOf(positions));
                        danmoInformationBar.setProfile(String.valueOf(profile));
                        danmoInformationBar.setInformationId(danmoInformation.getId());
//                        danmoInformationBarService.insertDanmoInformationBar(danmoInformationBar);
                        System.out.println("----------------------------------------");
                        System.out.println(danmoInformationBar);
                        // 更新上一行的中文名
                        previousName = String.valueOf(chineseName);
                        isFirstRow = false;
                    }
                }

                // 创建并添加 DanmoInformationContent
                String primaryTitle = row.getCell(16).getStringCellValue();
                String richText = row.getCell(17).getStringCellValue();
                String atlas1 = row.getCell(18).getStringCellValue();
                String sort1 = row.getCell(19).getStringCellValue();
                DanmoInformationContent danmoInformationContent = new DanmoInformationContent();
                danmoInformationContent.setInformationId(danmoInformation != null ? danmoInformation.getId() : null);
                danmoInformationContent.setParentHeading(primaryTitle);
                danmoInformationContent.setRichText(richText);
                danmoInformationContent.setAtlas(atlas1);
                danmoInformationContent.setSort(Long.valueOf(sort1));
                danmoInformationContents.add(danmoInformationContent);

                // 处理照片
                if (danmoInformation != null) {
                    DanmoInformationPhotos danmoInformationPhotos = new DanmoInformationPhotos();
                    danmoInformationPhotos.setInformationId(danmoInformation.getId());
                    danmoInformationPhotos.setPhotos(String.valueOf(avatar));
//                    danmoInformationPhotosServiceImpl.insertDanmoInformationPhotos(danmoInformationPhotos);
                    System.out.println("danmoInformationPhotos"+danmoInformationPhotos);
                }

                System.out.println("一级标题：" + primaryTitle);
                System.out.println("富文本：" + richText);
                System.out.println("图集（多张图片用逗号隔开）：" + atlas1);
                System.out.println("排序：" + sort1);
            }

            // 处理最后一组数据
            if (danmoInformation != null && !danmoInformationContents.isEmpty()) {
                System.out.println("danmoInformationContents"+danmoInformationContents);
//                danmoInformationContentService.add(danmoInformationContents);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success("添加成功");
    }

}
